package com.jams.goodox.widget;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnValueChangeListener;

import com.jams.goodox.R;

import java.util.List;

public class DateTimePicker extends FrameLayout {
	private final CustomNumberPicker mDateSpinner;
	private final CustomNumberPicker mHourSpinner;
	private OnDateTimeChangedListener mOnDateTimeChangedListener;

	List<String> dateList;
	List<String> timeZeroList;
	List<String> timeNormalList;

	public DateTimePicker(Context context, List<String> dateList,
                          List<String> timeZeroList, List<String> timeNormalList) {
		super(context);
		this.dateList = dateList;
		this.timeZeroList = timeZeroList;
		this.timeNormalList = timeNormalList;
		/**
		 * 加载布局
		 */
		inflate(context, R.layout.datedialog, this);
		/**
		 * 初始化控件
		 */
		mDateSpinner = (CustomNumberPicker) this.findViewById(R.id.np_date);
		mDateSpinner.setFocusable(true);
		mDateSpinner.setFocusableInTouchMode(true);
		mDateSpinner.setMinValue(0);
		mDateSpinner.setMaxValue(dateList.size() - 1);
		mDateSpinner.setDisplayedValues((String[]) dateList
				.toArray(new String[dateList.size()]));
		mDateSpinner.setOnValueChangedListener(mOnDateChangedListener);
		mDateSpinner.setNumberPickerDividerColor(mDateSpinner);
		EditText dataInput = findInput(mDateSpinner);
		dataInput.setFocusable(false);

		mHourSpinner = (CustomNumberPicker) this.findViewById(R.id.np_hour);
		mHourSpinner.setFocusable(true);
		mHourSpinner.setFocusableInTouchMode(true);
		mHourSpinner.setMaxValue(timeZeroList.size() - 1);
		mHourSpinner.setMinValue(0);
		mHourSpinner.setDisplayedValues((String[]) timeZeroList
				.toArray(new String[timeZeroList.size()]));
		mHourSpinner.setOnValueChangedListener(mOnHourChangedListener);
		mHourSpinner.setNumberPickerDividerColor(mHourSpinner);
		EditText hourInput = findInput(mHourSpinner);
		hourInput.setFocusable(false);

	}

	/**
	 * find spinner的子类eittext，禁止输入数据
	 * 
	 * @param np
	 * @return
	 */
	private EditText findInput(ViewGroup np) {
		int count = np.getChildCount();
		for (int i = 0; i < count; i++) {
			final View child = np.getChildAt(i);
			if (child instanceof ViewGroup) {
				findInput((ViewGroup) child);
			} else if (child instanceof EditText) {
				return (EditText) child;
			}
		}
		return null;
	}

	/**
	 * 公开用于设置选中服务时间方法
	 * @param date
	 * @param time
     */
	public void setDateTime(String date,String time){
		if(dateList==null||timeZeroList==null||timeNormalList==null)
			return;
		int datePosi=-1;
		int timePosi=-1;
		for(int i=0,len=dateList.size();i<len;i++){
			if(dateList.get(i).equals(date))
			{
				datePosi=i;
				break;
			}
		}
		if(datePosi<0)
			return;
		if(datePosi==0){
			for(int i=0,len=timeZeroList.size();i<len;i++){
				timePosi=i;
				setSelectPosi(datePosi,timePosi);
				break;
			}
		}else {
			for(int i=0,len=timeNormalList.size();i<len;i++){
				timePosi=i;
				setSelectPosi(datePosi,timePosi);
				break;
			}
		}
	}

	public void setSelectPosi(int datePosi,int timePosi){
		mDateSpinner.setValue(datePosi);
		mHourSpinner.setValue(timePosi);
	}

	/**
	 * 
	 * 控件监听器 注意onvaluechange里面的顺序不同，如果改变顺序会造成索引角标越界的bug
	 */
	private OnValueChangeListener mOnDateChangedListener = new OnValueChangeListener() {
		@Override
		public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
			if (newVal != 0) {
				mHourSpinner.setDisplayedValues((String[]) timeNormalList
						.toArray(new String[timeNormalList.size()]));
				mHourSpinner.setMaxValue(timeNormalList.size() - 1);
				mHourSpinner.setMinValue(0);
			} else {
				mHourSpinner.setMaxValue(timeZeroList.size() - 1);
				mHourSpinner.setMinValue(0);
				mHourSpinner.setDisplayedValues((String[]) timeZeroList
						.toArray(new String[timeZeroList.size()]));
			}
			/**
			 * 给接口传值
			 */
			onDateTimeChanged();
		}
	};

	private OnValueChangeListener mOnHourChangedListener = new OnValueChangeListener() {
		@Override
		public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
			onDateTimeChanged();
		}
	};

	/*
	 * 接口回调 参数是当前的View 年月日小时分钟
	 */
	public interface OnDateTimeChangedListener {
		void onDateTimeChanged(DateTimePicker view, String date, String hours);
	}

	/*
	 * 对外的公开方法
	 */
	public void setOnDateTimeChangedListener(OnDateTimeChangedListener callback) {
		mOnDateTimeChangedListener = callback;
	}

	private void onDateTimeChanged() {
		if (mOnDateTimeChangedListener != null) {
			if (mDateSpinner.getValue() == 0) {
				mOnDateTimeChangedListener.onDateTimeChanged(this,
						dateList.get(mDateSpinner.getValue()),
						timeZeroList.get(mHourSpinner.getValue()));
			} else {
				mOnDateTimeChangedListener.onDateTimeChanged(this,
						dateList.get(mDateSpinner.getValue()),
						timeNormalList.get(mHourSpinner.getValue()));
			}
		}
	}
}
